from google.appengine.api import urlfetch
import re,os


rNAME     =  r"([\w\d+& \/\-\'\.\*\&]*)"    # team name
rNICKNAME =  r"([\w\d+& \/\-\'\.\*\[\]]*)"    # team nick name


def GetTeams(url ):
    def Add(data):
      return ',"' + data + '"'

    result = urlfetch.fetch(url)
    scraped = result.content

#   Get Teams
    rexp = r"ref=teaminfo\.asp\?id=([\d]*)>([\w\d \/\.\(\\')]*)<"
    rexp = r"ref=teaminfo\.asp\?id=([\d]*)>" + rNICKNAME + "<"

    r = re.compile(rexp,re.IGNORECASE )
    match = r.findall( scraped )

    teamlist=[]

    for i,team in enumerate(match):
       teamlist.append( (i,team)   )

    return teamlist

def getTeamPlayers( teamid):

    url = "http://www.ustanorcal.com/teaminfo.asp?id="+ str(teamid)

    result = urlfetch.fetch(url)
    scraped = result.content

    rexp = r"href=playermatches\.asp\?id=([\d]*)>"    # id
    rexp += r"([\w'-]*)[ ,]([ \w'-]*)<[ \d\w\/<>=#]*>"   # LName, FName
    rexp += r"[ \d\w\/<>=#]*>"
    rexp += r"([\d\.\w]{3,4})"                        # NTRP
#   rexp += r"[ \d\w<>\/=#.]*align=center>"
#   rexp += r"([ \d]*)\/([ \d]*)"                     # Win/Loss


    r = re.compile(rexp,re.IGNORECASE )
    match = r.findall( scraped )
    retval = []
    for i,player in enumerate(match):
     uid = player[0]
     lname = player[1]
     fname = player[2]
     rating= player[3]
     retval.append( (uid,lname,fname,rating) )

    return retval


def getWinLossRecord( year,division,playerid):
    url = "http://www.ustanorcal.com/playermatches.asp?id="+playerid

    result = urlfetch.fetch(url)
    scraped = result.content

    rexp = r"ref=teaminfo\.asp\?id=([\d]*)>([\w\d \/\.\(\\')]*)<"
    rexp = r"("+year +" " + division + ")"
    rexp += r"[\t\r\n\d\w<>= \/\"\?]*"
    rexp += r"teaminfo\.asp\?id="+"([\d]*)" + '">'       # team id
    rexp += rNAME                                        # team name
    rexp += rNICKNAME                                    # team nick name (has [])
    rexp += r'[\t\r\n\d\w<>= \/\"\?]*?align=center>'     # non-greedy(two align=center)
    rexp += r"([ \d]*)" + r"[ \/]*" + r"([ \d]*)"        # win, loss

#   print rexp
    r = re.compile(rexp,re.IGNORECASE )
    match = r.findall( scraped )

    nl = "<br>"
#   print( str(len(match)) + " Matches ")
    record=[]
    for i,team in enumerate(match):
       league = team[0].strip()
       teamid = team[1].strip()
       teamname = team[2].strip()
       teamnickname = team[3].strip()
       win = team[4].strip()
       loss = team[5].strip()
       if(win==""):  win="0"
       if(loss==""):  loss="0"
       record.append((league,teamid,teamname,teamnickname,win,loss))

    return record


# Given players id, return USTA #  
def getUSTANumber(id):

    url  = "http://www.ustanorcal.com/playermatches.asp?id="+str(id) 

    result = urlfetch.fetch(url)
    scraped = result.content

    rexp = r"USTA[ #:]*<b>([\d]*)</b>"   
    r = re.compile(rexp,re.IGNORECASE )
    usta = r.findall( scraped )
    l = len(usta)
    return usta[0]


def LocalHost():
   if (re.search("roger",os.getcwd()) ): return True
   return False
